﻿//
// eCRAM, a monthly activity report web application
// Copyright (C) 2010  Aptea
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

using System.Data.Linq;
using System.Linq;
using eCRAM.Model;

namespace eCRAM.Dao
{
    /// <summary>
    /// Contains the list of all users available in the eCRAM application.
    /// </summary>
    /// 
    /// <remarks>
    /// An administrator can give eCRAM access to employes and subcontractors.
    /// A CRAM user has a username, a password, an id and can be active or inactive.
    /// A CRAM user also has a first name, familly name, its company name...
    /// 
    /// A CRAM user can define week ends, holidays, RTTs... inside its CRAM.
    /// He can also define its occupancy, values can be 0, 1/2, 1. This is the time
    /// that took the task during the day
    /// (0 = no work, 1/2 = half day work, 1 = full day work).
    /// </remarks>
    public class UserRepository : LinqRepository<User>
    {
        private Table<User> UsersTable { get; set; }

        public UserRepository(DataContext dataContent)
            : base(dataContent)
        {
             UsersTable = DataContext.GetTable<User>();
        }

        /// <summary>
        /// Get all users
        /// </summary>
        /// <returns></returns>
        public override IQueryable<User> GetAll()
        {
            return (from user in UsersTable select user);
        }

        /// <summary>
        /// Insert user
        /// </summary>
        /// <param name="user"></param>
        public override void Create(User user)
        {
            UsersTable.InsertOnSubmit(user);
            this.SubmitChanges();
        }

      /*  /// <summary>
        /// Update user
        /// </summary>
        /// <param name="user"></param>
        public override void Update(User user)
        {
            User tmp = this.GetById(user.Id);
            tmp.UserName = user.UserName;
            tmp.Password = user.Password;
            tmp.FirstName = user.FirstName;
            tmp.LastName = user.LastName;
            tmp.Company = user.Company;
            tmp.Email = user.Email;
            tmp.IsActive = user.IsActive;
            tmp.Role = user.Role;            
            this.SubmitChanges();
        }
       * */

        /// <summary>
        /// Delete user
        /// </summary>
        /// <param name="user"></param>
        public override void Delete(User user)
        {
            UsersTable.Attach(user);
            UsersTable.DeleteOnSubmit(user);
            this.SubmitChanges();
        }

        /// <summary>
        /// Get user by id
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public override  User GetById(System.Guid userId)
        {
            User user = (UsersTable.Where(p => p.Id == userId)).Single();
            return user;
        }

        /// <summary>
        /// Get only user list from start to limit 
        /// </summary>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <returns></returns>
        public override IQueryable<User> GetTop(int start, int end)
        {
            IQueryable<User> users = (                                        
                                        from user  
                                        in UsersTable 
                                        select user
                                     )
                                     .Skip(start)
                                     .Take(end - start);
            return users;
        }

        /// <summary>
        /// Get the Users Count in db
        /// </summary>
        /// <returns></returns>
        public override int GetCount()
        {
            int userCount = (from user in UsersTable select user).Count();
            return userCount;
        }

        /// <summary>
        /// Tell us if username is available to be createde or not (username must be unique)
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="userId">if userId is not Empty, we just check if username already exist 
        /// in db for an other user, if not check if user name exist in db</param>
        /// <returns></returns>
        public bool UserNameIsAvailable(string userName, System.Guid userId)
        {
            bool isAvailable = true;
            IQueryable<User> users;
            if (userId == System.Guid.Empty)
            {
                users = UsersTable.Where(u => u.UserName == userName);
            }
            else
            {
                users = UsersTable.Where(u => (u.UserName == userName) && (u.Id != userId));
            }

            if (users.Count() != 0)
            {
                isAvailable = false;
            }
            return isAvailable;
        }
    }
}
